<?php


namespace App\Http\Controllers\ProductCenter;

use App\Exceptions\ValidatorException;
use App\Http\Controllers\Controller;
use App\Http\Repositories\Repository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class CarouselController extends Controller
{
    private $repository;
    public function __construct(Repository $repository)
    {
        $this->repository = $repository;
    }

    public function getCarouselInfo(Request $request)
    {
        //验证规则
        $rules = [
            'id' => 'required|int',
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->getCarouselInfo($validator->getData());
    }

    public function getCarouselList(Request $request)
    {
        //验证规则
        $rules = [
            'operate' => 'required|int|in:1,2',
            'type' => 'nullable|string',
            'status' => 'nullable|int'
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->getCarouselList($validator->getData());
    }

    public function addCarousel(Request $request)
    {
        //验证规则
        $rules = [
            'type' => 'required|string',
            'title' => 'required|string',
            'describe' => 'nullable|string',
            'image_url' => 'required|string',
            'url' => 'required|url',
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->addCarousel($validator->getData());
    }

    public function editCarousel(Request $request)
    {
        //验证规则
        $rules = [
            'id' => 'required|int',
            'type' => 'nullable|string',
            'title' => 'nullable|string',
            'describe' => 'nullable|string',
            'image_url' => 'nullable|string',
            'url' => 'nullable|url',
            'sort' => 'nullable|int'
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->editCarousel($validator->getData());
    }

    public function delCarousel(Request $request)
    {
        //验证规则
        $rules = [
            'ids' => 'required'
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->delCarousel($validator->getData());
    }

}
